<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ include file="/WEB-INF/taglib.jsp"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<title>申报参数设置</title>
		<%@ include file="/WEB-INF/jsp/include.jsp"%>
		<script type="text/javascript" src="${path}/js/autoNumeric/autoNumeric.js"></script>
		<style type="text/css">
			 .numericInput {
				text-align:right;
			 }
		</style>		
		<script type="text/javascript">
		var id = "${id}";
		var sbcs_id = "${sbcs_id}";
		var ysxm_dm = "";
		var ysxm_mc = "";
		var ysxm_sl_ybjs = 0.00;
		var ysxm_sl_jyjs = 0.00;
		
		var numeric_options_bl_2 = {
				aSign: '%',
				pSign: 's',
				vMin: '0.00',
				vMax: '100.00'					
		};
		
		$(function(){
			setAjaxLoading();
			
			$("#txt_sl").val("");
			$("#txt_sl").autoNumeric('init', numeric_options_bl_2);
			
			$('#txt_ysxm_mc').searchbox({
				searcher:function(value,name) {
					var textbox = $('#txt_ysxm_mc').searchbox('textbox');
					if (textbox[0].disabled) {
						return;
					}
					
					parent.layer.open({
						  type: 2,
						  title: '选择应税项目',
						  shadeClose: false,
						  shade: 0.3,
						  area: ['800px', '500px'],
						  content: '${path}/jcxx/dmZzsYsxmmxb/sel_ysxm',
						  success: function(layero, index){
							  
						  },
						  end: function () {
							 		  
						  }
					});
				}
			});
			var textbox_ysxm = $('#txt_ysxm_mc').searchbox('textbox');
			textbox_ysxm.attr("readonly", "readonly");
			
			$("#sel_jsfs").combobox({
				onSelect: function(record) {
					var jsfs_dm = record.value;
					refresh_sl(jsfs_dm);
				}
			});
			
			$("#sel_yspb").combobox({
				onSelect: function(record) {
					$("#tr_ms")[0].style.display="none";
					$("#tr_js")[0].style.display="none";
					$("#tr_zcxm")[0].style.display="none";
					
					var dm = record.value;
					if ("04" == dm || "05" == dm || "06" == dm) {
						$("#txt_msxz_dm").val("");
						$("#txt_msxz_mc").val("");
						$("#tr_ms")[0].style.display="";
					} else if("07" == dm) {
						$("#txt_jsxz_dm").val("");
						$("#txt_jsxz_mc").val("");						
						$("#tr_js")[0].style.display="";
					} else if ("12" == dm) {
						$("#sel_zcxm").combobox("setValue", "-1");
						$("#tr_zcxm")[0].style.display="";
					}
				}
			});
			
			initGrid();
			
			clearForm();
			
			loadData();
		});

		function loadData() {			
			var url = '${path}/jcxx/sbcs/yssz/zzs/queryById';
			var param = new Object();
			param.id = id;
			
			$.ajax({
				type : "POST",
				url : url,
				dataType : "json",
				data: param,
				success : function callback(data) {
					if (data.code != '0000') {
						//$.messager.alert('错误', data.msg, 'error',function() {});
						$.alert("default", "error", data.msg);
					} else {
						var dto = data.returnObject.dto;
						var ysxmDto = data.returnObject.ysxmDto;
						
						ysxm_dm = ysxmDto.code;
						ysxm_mc = ysxmDto.name;
						if (ysxmDto.slYbjs != null) {
							ysxm_sl_ybjs = ysxmDto.slYbjs;
						}
						if (ysxmDto.slJyjs != null) {
							ysxm_sl_jyjs = ysxmDto.slJyjs;
						}
						
						$("#txt_ysxm_mc").searchbox("setValue", dto.ysxmMc);
						$("#sel_yspb").combobox("setValue", dto.yspbDm);
						$("#sel_jsfs").combobox("setValue", dto.jsfsDm);
						$("#txt_sl").autoNumeric('set', dto.sl * 100);
						$("#txt_szsm")[0].value = dto.szsm;
						
						var dm = dto.yspbDm;
						if ("04" == dm || "05" == dm || "06" == dm) {
							$("#txt_msxz_dm").val(dto.msxzDm);
							$("#txt_msxz_mc").val(dto.msxzMc);
							$("#tr_ms")[0].style.display="";
						} else if("07" == dm) {
							$("#txt_jsxz_dm").val(dto.jsxzDm);
							$("#txt_jsxz_mc").val(dto.jsxzMc);						
							$("#tr_js")[0].style.display="";
						} else if ("12" == dm) {
							$("#sel_zcxm").combobox("setValue", dto.zcxmDm);
							$("#tr_zcxm")[0].style.display="";
						}
						
						//会计科目
						var rows = new Array();
						
						var row = new Object();
						row.zt_id = dto.ztId;
						row.zt_name = dto.ztMc;
						row.ssnd = dto.ssnd;
						row.kjkm_id = dto.kmId;
						row.kjkm_code = dto.kmDm;
						row.kjkm_qc = dto.kmQc;
						row.hsxm_id = dto.hsxmId;
						row.hsxm_mc = dto.hsxmMc;
						
						rows.push(row);
						
						$("#tt").datagrid("loadData", rows);
					}
				},
				error : function(data, textstatus) {
					//$.messager.alert('错误', data.responseText, 'error',function(){});
					$.alert("default", "error", data.responseText);
				}
			});			
		}
		
		function initGrid() {
			$('#tt').datagrid({
				fit:true,
				nowrap:true,
				striped: true,
				singleSelect:true,
				pagination:false,
				rownumbers:true,
				idField: "kjkm_id",
				toolbar:'#tb',
				fitColumns:false,
				columns:[[
					{title:'数据源',field:'zt_name',width:'195',align:'left',halign:'center',sortable:false,resizable: true},
					{title:'年度',field:'ssnd',width:'70',align:'center',halign:'center',sortable:false,resizable: true},
					{title:'对应会计科目名称',field:'kjkm_qc',width:'300',align:'left',halign:'center',sortable:false,resizable: true},
					{title:'对应会计科目代码',field:'kjkm_code',width:'130',align:'left',halign:'center',sortable:false,resizable: true},
					{title:'核算项目',field:'hsxm_mc',width:'200',align:'left',halign:'center',sortable:false,resizable: true}
				]],
				onLoadSuccess:function(){
					$("#tt").datagrid("clearSelections");
					$("#tt").datagrid("fixRownumber");
				}
			});
			
			$('#tt').datagrid("loadData", []);
		}
		
		function clearForm() {
			$("#txt_ysxm_mc").searchbox("clear");
			$("#sel_yspb").combobox("setValue", "-1");
			$("#sel_jsfs").combobox("setValue", "-1");
			
			$("#txt_msxz_dm").val("");
			$("#txt_msxz_mc").val("");
			
			$("#txt_jsxz_dm").val("");
			$("#txt_jsxz_mc").val("");
			
			$("#sel_zcxm").combobox("setValue", "-1");
			
			$("#txt_szsm")[0].value = "";
			
			$("#tt").datagrid("loadData", []);
		}
		
		function getValue() {
			var yspb_dm = $("#sel_yspb").combobox("getValue");
			var jsfs_dm = $("#sel_jsfs").combobox("getValue");
			
			var sl = 0.00;
			var str_sl = $("#txt_sl").autoNumeric('get');
			if (str_sl != "") {
				sl = parseFloat(str_sl) / 100;
			}
			
			if ("04" == yspb_dm || "05" == yspb_dm || "06" == yspb_dm) {
				var msxz_dm = $("#txt_msxz_dm").val();
				var msxz_mc = $("#txt_msxz_mc").val();
			} else if("07" == yspb_dm) {
				var jsxz_dm = $("#txt_jsxz_dm").val();
				var jsxz_mc = $("#txt_jsxz_mc").val();
			} else if ("12" == yspb_dm) {
				var zcxm_dm = $("#sel_zcxm").combobox("getValue");
			}
			
			var szsm = $("#txt_szsm")[0].value;
			
			var rows = $("#tt").datagrid("getRows");
			
			var param = new Object();
			param.id = id;
			param.sbcsId = sbcs_id;
			
			param.ysxmDm = ysxm_dm;
			param.yspbDm = yspb_dm;
			param.jsfsDm = jsfs_dm;
			param.sl = sl;
			
			if ("04" == yspb_dm || "05" == yspb_dm || "06" == yspb_dm) {
				param.msxzDm = msxz_dm;
				param.msxzMc = msxz_mc;
			} else if("07" == yspb_dm) {
				param.jsxzDm = jsxz_dm;
				param.jsxzMc = jsxz_mc;
			} else if ("12" == yspb_dm) {
				param.zcxmDm = zcxm_dm;
			}
			
			param.szsm = szsm;
			
			param.kjkm_count = rows.length;
			var item = rows[0];
			param[String.format("kjkm_{0}_zt_id", 1)] = item.zt_id;
			param[String.format("kjkm_{0}_ssnd", 1)] = item.ssnd;
			param[String.format("kjkm_{0}_kmId", 1)] = item.kjkm_id;
			param[String.format("kjkm_{0}_kmDm", 1)] = item.kjkm_code;
			param[String.format("kjkm_{0}_kmQc", 1)] = item.kjkm_qc;
			param[String.format("kjkm_{0}_hsxmId", 1)] = item.hsxm_id;
			
			return param;
		}
		
		function refresh_sl(jsfs_dm) {
			$("#txt_sl").val("");
			if (ysxm_dm != "") {
				if ("1" == jsfs_dm ) {
					$("#txt_sl").autoNumeric('set', ysxm_sl_ybjs * 100);
				} else if("2" == jsfs_dm) {
					$("#txt_sl").autoNumeric('set', ysxm_sl_jyjs * 100);
				}
			}
		}
		
		//保存数据
		function submit_Edit(){
			if (!checkInput()) {
				return;
			}
			
			var url = '${path}/jcxx/sbcs/yssz/zzs/update';
			var param = getValue();
			
			$.ajax({
				type : "POST",
				url : url,
				dataType : "json",
				data: param,
				success : function callback(data) {
					if (data.code != '0000') {
						//$.messager.alert('错误', data.msg, 'error',function() {});
						$.alert("update", "error", "");
					} else {
						//$.messager.alert("提示", "保存成功。", "info",function(){
						//	parent.btnQuery_yssz();
 						//	setTimeout("closeLayer()",100);
						//});
						$.alert("update", "info", "");
					}
				},
				error : function(data, textstatus) {
					//$.messager.alert('错误', data.responseText, 'error',function(){});
					$.alert("default", "error", data.responseText);
				}
			});
		}
		
		function checkInput() {
			if (ysxm_dm == null || ysxm_dm == "") {
				//$.messager.alert('提示', '应税项目不能为空，请选择应税项目。', 'info',function(){});
				$.alert("pick", "info", "应税项目");
				return false;
			}
			
			var yspb_dm = $("#sel_yspb").combobox("getValue");
			if (yspb_dm == null || yspb_dm.length == 0 || yspb_dm == "null" || yspb_dm == "-1") {
				//$.messager.alert('提示', '应税判别不能为空，请选择应税判别。', 'info',function(){});
				$.alert("pick", "info", "应税判别");
				return false;
			}
			
			var jsfs_dm = $("#sel_jsfs").combobox("getValue");
			if (jsfs_dm == null || jsfs_dm.length == 0 || jsfs_dm == "null" || jsfs_dm == "-1") {
				//$.messager.alert('提示', '计税方式不能为空，请选择计税方式。', 'info',function(){});
				$.alert("pick", "info", "计税方式");
				return false;
			}
			
			if ("04" == yspb_dm || "05" == yspb_dm || "06" == yspb_dm) {
				var msxz_dm = $("#txt_msxz_dm").val();
				if (msxz_dm == null || msxz_dm.length == 0) {
					//$.messager.alert('提示', '免税性质代码不能为空。', 'info',function(){});
					$.alert("default", "info", "免税性质代码不能为空！");
					return false;
				}
				
				var msxz_mc = $("#txt_msxz_mc").val();
				if (msxz_mc == null || msxz_mc.length == 0) {
					//$.messager.alert('提示', '免税性质名称不能为空。', 'info',function(){});
					$.alert("default", "info", "免税性质名称不能为空！");
					return false;
				}
			} else if("07" == yspb_dm) {
				var jsxz_dm = $("#txt_jsxz_dm").val();
				if (jsxz_dm == null || jsxz_dm.length == 0) {
					//$.messager.alert('提示', '减税性质代码不能为空。', 'info',function(){});
					$.alert("default", "info", "减税性质代码不能为空！");
					return false;
				}
				
				var jsxz_mc = $("#txt_jsxz_mc").val();
				if (jsxz_mc == null || jsxz_mc.length == 0) {
					//$.messager.alert('提示', '减税性质名称不能为空。', 'info',function(){});
					$.alert("default", "info", "减税性质名称不能为空！");
					return false;
				}
			} else if ("12" == yspb_dm) {
				var zcxm_dm = $("#sel_zcxm").combobox("getValue");
				if (zcxm_dm == null || zcxm_dm.length == 0 || zcxm_dm == "null" || zcxm_dm == "-1") {
					//$.messager.alert('提示', '转出项目不能为空，请选择转出项目。', 'info',function(){});
					$.alert("pick", "info", "转出项目");
					return false;
				}
			}
			
			var rows = $("#tt").datagrid("getRows");
			if (rows == null || rows.length == 0) {
				//$.messager.alert('提示', '会计科目不能为空，请选择会计科目。', 'info',function(){});
				$.alert("pick", "info", "会计科目");
				return false;
			}
			
			return true;
		}
		
		function btnEditKjkm() {
			parent.open_yssz_zzs_edit_kjkm(sbcs_id);		
		}
		
		function btnEditHsxm() {
			var row = $('#tt').datagrid('getSelected');
		    if (row == null) {
		        //$.messager.alert("友情提醒", "请先选择要修改的会计科目！", "info");
		        $.alert("pick", "info", "会计科目");
		        return;
		    }
		    
		    var km_id = row.kjkm_id;
			parent.open_yssz_zzs_hsxm(km_id);
		}		
		</script>
	</head>
  <body  style="margin:0px; padding:0px;" > 
	<%@ include file="/WEB-INF/jsp/loading.jsp" %>
	<div class="easyui-layout" fit="true" >
		<div data-options="region:'center',title:'',split:false,border: false">
			<div align="center" style="padding-top: 10px; padding-bottom: 10px;">
					<form id="sbcsForm" method="post" style="margin-bottom: 5px;">
						<table class="f_table" cellspacing="1">
							<colgroup> 
								<col style="width:155;"></col>
								<col style="width:215;"></col>
								<col style="width:155;"></col>
								<col style="width:215;"></col>								
							</colgroup>					
							<tr>
								<td align="right" class="s_bg">
									应税项目：
								</td>
								<td align="left">
									<input id="txt_ysxm_mc" class="easyui-searchbox" style="width:214px" data-options="editable:false,height:35"></input>
								</td>
								<td align="right" class="s_bg">
									应税判别：
								</td>
								<td align="left">
									<select id="sel_yspb" class="easyui-combobox" style="width:214px;" data-options="editable:false,height:35">
										<option value="-1" selected="selected">请选择</option>
										<c:forEach var="item"  items="${yspbList}">
											<option value="${item.code}">${item.name}</option>
										</c:forEach>									
									</select>									
								</td>
							</tr>							
							<tr>
								<td align="right" class="s_bg">
									计税方式：
								</td>					
								<td align="left">
									<select id="sel_jsfs" class="easyui-combobox" style="width:214px;" data-options="editable:false,height:35">
										<option value="-1" selected="selected">请选择</option>
										<c:forEach var="item"  items="${jsfsList}">
											<option value="${item.code}">${item.name}</option>
										</c:forEach>									
									</select>
								</td>								
								<td align="right" class="s_bg">
									税率：
								</td>
								<td align="left">
									<input type="text" id="txt_sl" class="numericInput" style="width:214px;" readonly="readonly">										
								</td>															
							</tr>
							<tr id="tr_ms" style="display: none;">
								<td align="right" class="s_bg">
									免税性质代码：
								</td>
								<td align="left">
									<input type="text" id="txt_msxz_dm" style="width:214px;">										
								</td>															
								<td align="right" class="s_bg">
									免税性质名称：
								</td>
								<td align="left">
									<input type="text" id="txt_msxz_mc" style="width:214px;">										
								</td>															
							</tr>
							<tr id="tr_js" style="display: none;">
								<td align="right" class="s_bg">
									减税性质代码：
								</td>
								<td align="left">
									<input type="text" id="txt_jsxz_dm" style="width:214px;">										
								</td>															
								<td align="right" class="s_bg">
									减税性质名称：
								</td>
								<td align="left">
									<input type="text" id="txt_jsxz_mc" style="width:214px;">										
								</td>															
							</tr>
							<tr id="tr_zcxm" style="display: none;">
								<td align="right" class="s_bg">
									转出项目：
								</td>					
								<td align="left" colspan="3">
									<select id="sel_zcxm" class="easyui-combobox" style="width:214px;" data-options="editable:false,height:35">
										<option value="-1" selected="selected">请选择</option>
										<c:forEach var="item"  items="${zcxmList}">
											<option value="${item.code}">${item.name}</option>
										</c:forEach>									
									</select>
								</td>
							</tr>
							<tr>
								<td align="right" class="s_bg">设置说明：</td>
								<td align="left" colspan="3">
									<textarea id="txt_szsm" rows="" cols="" style="width: 587px; height: 80px;"></textarea>
								</td>
							</tr>																												
						</table>
					</form>
					
					<div style="width: 752px; height: 150px;">
						<table id="tt" data-options="border:true,title:'对应会计科目',fit:true"></table>
					</div>  
				</div>
		</div>
		<div data-options="region:'south',title:'',split:false,border: false" style="height:55px;">
			<div align="center" style="margin-top: 15px;">
		        	<a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-ok" onclick="submit_Edit()">确定</a>
		        	<a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-cancel" onclick="closeLayer();">取消</a>
       		</div>
		</div>
	</div>
    <div id="tb" style="padding: 1px; height: auto; display: none;">
        <div>
        	<a id="btn_add_tb" href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-edit" plain="true" onclick="btnEditKjkm()">修改</a>
        	<a id="btn_hsxm_tb" href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-search" plain="true" onclick="btnEditHsxm()">核算项目</a>
        </div>
    </div>		
</body>
</html>
